/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.sanerzone.smscenter.modules.sms.web;
import com.sanerzone.jeebase.modules.sys.utils.UserUtils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.sanerzone.jeebase.common.config.Global;
import com.sanerzone.jeebase.common.persistence.Page;
import com.sanerzone.jeebase.common.web.BaseController;
import com.sanerzone.jeebase.common.utils.StringUtils;
import com.sanerzone.smscenter.modules.sms.entity.UnsubKeyword;
import com.sanerzone.smscenter.modules.sms.service.UnsubKeywordService;

/**
 * 退订关键字管理Controller
 * @author xql
 * @version 2018-04-15
 */
@Controller
@RequestMapping(value = "${adminPath}/sms/unsubKeyword")
public class UnsubKeywordController extends BaseController {

	@Autowired
	private UnsubKeywordService unsubKeywordService;
	
	@ModelAttribute
	public UnsubKeyword get(@RequestParam(required=false) String id) {
		UnsubKeyword entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = unsubKeywordService.get(id);
		}
		if (entity == null){
			entity = new UnsubKeyword();
		}
		return entity;
	}
	
	@RequiresPermissions("sms:unsubKeyword:view")
	@RequestMapping(value = {"list", ""})
	public String list(UnsubKeyword unsubKeyword, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<UnsubKeyword> page = unsubKeywordService.findPage(new Page<UnsubKeyword>(request, response), unsubKeyword); 
		model.addAttribute("page", page);
		return "modules/sms/unsubKeywordList";
	}

	@RequiresPermissions("sms:unsubKeyword:view")
	@RequestMapping(value = "form")
	public String form(UnsubKeyword unsubKeyword, Model model) {
		model.addAttribute("unsubKeyword", unsubKeyword);
		return "modules/sms/unsubKeywordForm";
	}

	@RequiresPermissions("sms:unsubKeyword:edit")
	@RequestMapping(value = "save")
	public String save(UnsubKeyword unsubKeyword, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, unsubKeyword)){
			return form(unsubKeyword, model);
		}
		unsubKeyword.setOperater(UserUtils.getUser().getName());
		unsubKeywordService.save(unsubKeyword);
		addMessage(redirectAttributes, "保存退订关键字成功");
		return "redirect:"+Global.getAdminPath()+"/sms/unsubKeyword/?repage";
	}
	
	@RequiresPermissions("sms:unsubKeyword:edit")
	@RequestMapping(value = "delete")
	public String delete(UnsubKeyword unsubKeyword, RedirectAttributes redirectAttributes) {
		unsubKeywordService.delete(unsubKeyword);
		addMessage(redirectAttributes, "删除退订关键字成功");
		return "redirect:"+Global.getAdminPath()+"/sms/unsubKeyword/?repage";
	}

}